;;; init-java.el --- Configuration for Java programming -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:

(global-set-key (kbd "C-h j") #'javadoc-lookup)
(with-eval-after-load 'cc-mode
  (define-key java-mode-map (kbd "C-c c") #'dap-debug)
  (define-key-with-arg java-mode-map (kbd "C-c a") ctrlwei/dap-debug "jca")
  (define-key-with-arg java-mode-map (kbd "C-c e") ctrlwei/dap-debug "jd-et"))

(add-hook 'java-mode-hook (lambda ()
                            (unless (eq major-mode 'jdecomp-preview-mode)
                              (require 'lsp-java)
                              (lsp))))

(with-eval-after-load 'java-ts-mode
  (require 'cc-mode)
  (setq java-ts-mode-hook java-mode-hook)
  (setq java-ts-mode-map java-mode-map))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; lsp-java
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; configuration for install jdtls when GFW block github.
;; (setq lsp-java-jdt-download-url "https://mirrors.tuna.tsinghua.edu.cn/eclipse/jdtls/snapshots/jdt-language-server-latest.tar.gz")
;; (setq lsp-java--download-root "https://mirror.ghproxy.com/https://raw.githubusercontent.com/emacs-lsp/lsp-java/master/install/")

(setq lsp-java-format-settings-url (expand-file-name "eclipse-java-google-style.xml"
                                                     ctrlwei-etc-dir))
(setq lsp-java-format-settings-profile "GoogleStyle")
(setq lsp-java-save-actions-organize-imports nil)


(with-eval-after-load 'jdecomp
  (setq jdecomp-decompiler-paths `((cfr . , (expand-file-name "cfr-0.146.jar"
                                             ctrlwei-third-party-bin-dir)))))

(defun jdecomp-view (file)
  "Decompile FILE and view buffer of decompiled contents."
  (interactive (list (buffer-file-name)))
  (pcase-let ((`(,jar ,file) (split-string file ":")))
    (if file
        (progn
          (kill-buffer (current-buffer))
          (jdecomp-decompile-and-view file jar))
      (progn
        (kill-buffer (current-buffer))
        (jdecomp-decompile-and-view jar)))))


(with-eval-after-load 'javadoc-lookup
  (setq javadoc-lookup-completing-read-function #'completing-read)
  (javadoc-add-roots "/usr/share/doc/java11-openjdk/api"))

(provide 'init-java)
;;; init-java.el ends here
